from datetime import datetime
from enum import unique
from typing import Tuple
from django.db import models
from django.db.models import indexes
from django.db.models.deletion import CASCADE
from django.db.models.indexes import Index
from django.utils import timezone


class Patients(models.Model):  # 病人注册信息
    id_patient = models.CharField(
        max_length=50, primary_key=True, verbose_name='id', db_column='id')  # db_column设置表的列名
    name_patient = models.CharField(max_length=20, db_column='name')
    sex_patient = models.IntegerField(
        choices=((0, '男'), (1, '女')), default=0, db_column='sex')  # '男''女'是在页面中显示的
    age_patient = models.IntegerField(null=True, db_column='age')
    contact_patient = models.CharField(
        max_length=20, null=True, db_column='contact')
    registerTime_patient = models.DateTimeField(
        '%Y-%m-%d %H:%M:%S', auto_now_add=True, db_column='registerTime')  # 登陆时间自动填充

    class Meta:
        verbose_name = '病人注册信息表'


class Doctors(models.Model):  # 医生信息 后台加入
    id_doctor = models.CharField(max_length=50, primary_key=True)
    name_doctor = models.CharField(max_length=20)
    sex_doctor = models.IntegerField(
        choices=((0, '男'), (1, '女')), default=0)  # '男''女'是在页面中显示的
    age_doctor = models.IntegerField(null=True)
    type_doctor = models.CharField(max_length=10, choices=(
        ('emergency', '急诊'), ('department', '科室'), ('assay', '化验'), ('treat', '治疗'), ('impatient', '住院'))
    )
    workplace_doctor = models.CharField(max_length=20, choices=(
        ('内科', '内科'), ('外科', '外科'), ('骨科', '骨科'), ('妇科',
                                                   '妇科'), ('眼科', '眼科'), ('皮肤科', '皮肤科'),
        ('中医科', '中医科'), ('疼痛科', '疼痛科'), ('口腔科',
                                         '口腔科'), ('耳鼻喉科', '耳鼻喉科'), ('急诊科', '急诊科'),
        ('产科', '产科'), ('传染科', '传染科'),('化验科','化验科'))#化验科 验血 X光 核磁共振
    )  # 工作科室 内科 外科
    consulting_room = models.CharField(max_length=50)  # 诊室 具体的内科中的哪一科
    pwd_doctor = models.CharField(max_length=20)  # 医生的登录密码 入职时由后台确定

    class Meta:
        verbose_name = '医生信息注册表'


class Case(models.Model):  # 病例本 在第一次医生操作时创建
    patient = models.ForeignKey('Patients', on_delete=CASCADE, null=False)
    doctors = models.ForeignKey('Doctors', on_delete=CASCADE, null=False)
    diagnoseTime = models.DateTimeField('%Y-%m-%d %H:%M:%S', auto_now_add=True)
    text = models.TextField(max_length=1000)
    name_doctor = models.CharField(max_length=20)
    name_patient=models.CharField(max_length=50)
    type_doctor = models.CharField(max_length=20, choices=(
        ('emergency', '急诊'), ('department', '科室'), ('assay', '化验'), ('treat', '治疗'), ('impatient', '住院'))
    )
    InP = models.CharField(max_length=5, choices=(
        ('非住院', '不住院'), ('住院', '住院')
    ))
    Surgery = models.CharField(max_length=5, choices=(
        ('非手术', '不手术'), ('手术', '手术')
    ))

    class Meta:
        unique_together = (('patient', 'doctors', 'diagnoseTime'),)  # 主键
        verbose_name = '病例表'
        indexes = [models.Index(fields=['patient']), ]


class DoctorsWork(models.Model):  # 医生工作时间表 用于挂号
    id_doctor = models.ForeignKey('Doctors', on_delete=CASCADE, null=False) #沿用models中原字段名
    work_time = models.DateField('%Y-%m-%d')  # 工作时间
    time_quantum = models.CharField(max_length=5, choices=(
        ('AM', '上午'), ('PM', '下午')
    ))  # 时间段 上午 下午
    level = models.CharField(max_length=20, null=True, blank=True, choices=(
        ('expert', '专家'), ('normal', '普通')
    ))
    num_total = models.IntegerField(null=False)
    num_remain = models.IntegerField(null=False)

    class Meta:
        unique_together = (('id_doctor', 'work_time', 'time_quantum'),)
        verbose_name = '医生工作排班表'
        indexes = [models.Index(fields=['id_doctor']), ]


class PatientLine(models.Model):  # 用于挂号排队的表
    id_doctor = models.ForeignKey(
        'Doctors', on_delete=CASCADE, null=False)  # 所挂号的医生 此字段名多次使用沿用mysql中的
    name_doctor = models.CharField(max_length=20)
    id_patient = models.ForeignKey(
        'Patients', on_delete=CASCADE, null=False)  # 所挂号的病人 此段名多次使用沿用msql中的
    name_patient = models.CharField(max_length=20)
    work_time = models.DateField('%Y-%m-%d')  # 所挂号的日期
    time_quantum = models.CharField(max_length=5, choices=(
        ('AM', '上午'), ('PM', '下午')
    ))  # 时间段 上午 下午
    regisTime = models.DateTimeField(
        '%Y-%m-%d %H:%M:%S', auto_now_add=True, db_column='regisTime')  # 挂号时间自动填充

    class Meta:
        unique_together = (
            ('id_doctor', 'id_patient', 'work_time', 'time_quantum'),)
        verbose_name = '挂号队列表'
        ordering = ["regisTime"]
        indexes = [models.Index(fields=['work_time', 'id_doctor']), ]


class AssayLine(models.Model):  # 验血排队表
    id_patient = models.OneToOneField(
        'Patients',  on_delete=CASCADE)
    remark=models.TextField(max_length=1000,null=True)
  


class MriLine(models.Model):  # 核磁共振排队表
    id_patient = models.OneToOneField(
        'Patients', on_delete=CASCADE)
    remark=models.TextField(max_length=1000,null=True)
    



class XRayLine(models.Model):  # x光排队表
    id_patient = models.OneToOneField(
        'Patients',  on_delete=CASCADE)
    remark=models.TextField(max_length=1000,null=True)
    
    


class OPPharmacy(models.Model):  # 门诊部药房库存
    nd_code = models.CharField(max_length=100, primary_key=True)
    name_drug = models.CharField(max_length=100)
    manufacturer = models.CharField(max_length=100)
    storage = models.IntegerField()  # 库存
    units_drug = models.CharField(max_length=10)  # 药的单位 支 个 盒 瓶


class OPDispensary(models.Model):  # 药房取药处应发药表单
    id_patient = models.ForeignKey('Patients', on_delete=CASCADE)
    id_doctor = models.ForeignKey('Doctors', on_delete=CASCADE)
    system_time = models.DateTimeField(auto_now_add=True)
    nd_code = models.ForeignKey(OPPharmacy, on_delete=CASCADE)
    name_drug = models.CharField(max_length=100)
    num_drug = models.IntegerField()

    class Meta:
        unique_together = (('id_patient', 'id_doctor', 'system_time'),)


